#!/bin/bash
# Author： Syuan-Rong Chen
# E-mail： calvin.atx@gmail.com
# Blog：   http://calvinit.blogspot.com
# Date : 20100815
#
# <description>
# Copyright (C) <year>  <name> <email>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see .
#
drvm=$HOME/calvin/cvirt/virt_vm.txt
vmnum=$(wc $HOME/calvin/cvirt/virt_vm.txt|fmt -u|cut -d' ' -f3)

clear

function ck_vmtp {
	tp=1
	s=$(cat $drvm)
	for us in $s	
	do
		t1[tp]=${us%%:*}
		t2[tp]=${us##*:}
		t3[tp]=${t1[tp]}${t2[tp]}.xml
		[ ! -e ${t3[tp]} ] && virsh dumpxml ${t2[tp]} > ${t3[tp]}
		cdate=$(ls -l ${t3[tp]} | cut -d' ' -f6)
		ctime=$(ls -l ${t3[tp]} | cut -d' ' -f7)
		echo -e [$tp] $cdate $ctime"\t"${t2[tp]}  
		tp=$(( $tp + 1 ))
	done
}

function ck_vmast {
	s=$(cat $drvm)

	for us in $s
	do
		u1=${us%%:*}
		u2=${us##*:}
		u3=$u1$u2.xml

		virsh dumpxml $u2 > $u3
		echo -e '\E[1;31m' 主機:$u2 設定已備份到 $u3 ; tput sgr0
	done
}

function outputMenu {
echo -e '\E[1;33m'"歡迎使用Calvin_Virt Manager"; tput sgr0
echo ""
echo -e '\E[1;32m'"請選擇要備份的虛擬機器"; tput sgr0
echo ""
echo -e [id] 上次備份"\t\t"主機名稱
echo "---------------------------"
ck_vmtp
echo ""
echo "[a] 全部備份"
echo "[0] 回上一層"

echo ""
echo -ne "請選擇: "
read USERCHOICE
clear

case $USERCHOICE in
	"a") ck_vmast ;;
	"0") . /usr/cbin/d-cvirt/vm.sh ;;
        $USERCHOICE) 
		virsh dumpxml ${t2[$USERCHOICE]} > ${t3[$USERCHOICE]} 
		echo -e '\E[1;31m' 主機:${t2[$USERCHOICE]} 設定已備份到 ${t3[$USERCHOICE]} ; tput sgr0
		;;
        *) echo "請輸入正確的選擇";;
esac
}

. /usr/cbin/c/domenu.sh
